<template>
	<view class="app-container">
		<view class="content" v-if="applyList.length">
			<cell-friend v-for="item in applyList" :key="item.id" :title="item.nickname" @click.native>
				<template v-slot:left>
					<image class="icon-friend" :src="item.avatar ? item.avatar : defaultAvatar"></image>
				</template>
				<template v-slot:right>
					<button type="primary" size="mini" class="agree-btn" @click="handleAgree(item.id)">同意</button>
					<button type="default" size="mini" @click="handleReject(item.id)">拒绝</button>
				</template>
			</cell-friend>
		</view>
		<view v-else class="none">目前无好友申请</view>

	</view>
</template>

<script>
	import {
		getUser
	} from '../../utils/auth.js'
	import CellFriend from '../../components/cell-friend.vue'
	export default {
		components: {
			CellFriend
		},
		data() {
			return {
				defaultAvatar: require("../../static/images/avatar.png"),
				user: {},
				applyList: [],
			}
		},
		onLoad(options) {
			this.user = getUser()
			this.getApplyList()
		},
		methods: {
			getApplyList() {
				this.$http('/admin/chat/friendApply/getListByUserId/' + this.user.userInfo.userId).then(res => {
					this.applyList = res.data.list
				})
			},
			handleAgree(fromId) {
				let jsonData = {
					fromId,
					toId:this.user.userInfo.userId
				}
				this.$http('/admin/chat/friendApply/agree','GET',jsonData).then(res =>{
					uni.showToast({
						title:res.message
					})
					setTimeout(()=>{
						uni.navigateBack()
					},500)
				})
			},
			handleReject(fromId) {
				let jsonData = {
					fromId,
					toId:this.user.userInfo.userId
				}
				this.$http('/admin/chat/friendApply/reject','GET',jsonData).then(res =>{
					uni.showToast({
						title:res.message
					})
					setTimeout(()=>{
						uni.navigateBack()
					},500)
				})
			},
		}
	}
</script>

<style>
	.agree-btn {
		margin-right: 10px;
	}
	.none{
		padding: 20px;
	}
</style>
